<template>
  <keep-alive :include="cachedViews">
    <div>
      <div class="app-container">
        <div class="header">{{$route.meta.title}}</div>
      </div>
      <div class="app-container margin-content padding-content">
        <!--<el-header >Header</el-header >-->
        <div class="filter-container clearfix">
          <el-button class="filter-item" icon="el-icon-edit" type="primary" @click="handleCreate">新建机构
          </el-button>
          <!--<el-button class="filter-item" type="danger" icon="el-icon-delete" >删除</el-button >-->
          <div class="fr">
            <el-input placeholder="请输入机构名称" style="width:200px;" v-model="query"
                      @keyup.native="doSearch">
              <i slot="prefix" class="el-input__icon el-icon-search"></i>
              <!--<template slot="append" >-->
              <!--<el-button icon="el-icon-search" @click="doSearch" ></el-button >-->
              <!--</template >-->
            </el-input>
            <!--<el-button class="filter-item" icon="el-icon-search" type="primary" >搜索</el-button >-->
          </div>
        </div>

        <tree-table :height="scrollHeight" :data="data" :evalFunc="func" :evalArgs="args" :expandAll="expandAll" border
                    :labelName="'机构名称'">
          <!--<el-table-column-->
          <!--type="selection"-->
          <!--slot="firstColumn"-->
          <!--width="55" >-->
          <!--</el-table-column >-->
          <el-table-column label="操作" width="200"  fixed="right">
            <template slot-scope="scope">
              <el-button type="text" @click="handleEdit(scope.row.id)">编辑</el-button>
              <divider type="vertical"></divider>
              <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </tree-table>
      </div>

      <!--新建用户 Dialog-->
      <new-org-dialog ref="newOrgDialog" @submited-menu="submitedMenu" :orgOptions="orgianlData"></new-org-dialog>
    </div>
  </keep-alive>
</template>

<script>
  import treeTable from '@/components/TreeTable'
  import treeToArray from './customEval'
  import { getOrgTree, delOrg } from '@/api/orgs'
  import newOrgDialog from './components/newOrgDialog.vue'
  import { modifyObj, filterMenus } from '@/utils/tools'
  import Divider from '@/components/Divider'

  export default {
    name: 'orgMain',
    computed: {
      cachedViews() {
        return this.$store.state.tagsView.cachedViews
      }
    },
    components: {treeTable, newOrgDialog, Divider},
    data() {
      return {
        listLoading: false,
        tableKey: 0,
        data: [],//机构树数据
        orgianlData: [],//原始机构树数据
        scrollHeight: null,
        query: '',
        func: treeToArray,
        expandAll: true,
        args: [null, null, 'timeLine']
      }
    },
    created() {
      this.getOrgTree()
      this.tableResize()
    },
    methods: {
      tableResize(){
        const _height = 285
        this.scrollHeight = document.body.scrollHeight - _height
        window.onresize = () => {
          this.scrollHeight = document.body.scrollHeight - _height
        }
      },
      getOrgTree() {
        getOrgTree().then(res => {
          res = res.data
          this.orgianlData = res.data
          this.data = res.data
        })
      },
      message(row) {
        this.$message.info(row.name)
      },
      handleCreate() {
        this.$refs.newOrgDialog.handleCreate()
      },
      handleEdit(id) {
        this.$refs.newOrgDialog.handleEdit(id)
      },
      handleDelete(obj) {
        if (obj.children !== undefined) {
          this.$alert('该节点下存在子机构，不能删除，请先删除子机构', '提示', {
            confirmButtonText: '确定',
          })
          return false
        }
        this.$confirm('此操作将永久删除该机构, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delOrg({ids: obj.id}).then(res => {
            res = res.data
            this.submitedMenu()
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      doSearch() {
        if (this.query !== '') {
          let result = filterMenus(this.orgianlData, this.query)
          this.data = result
        } else {
          this.getOrgTree()
        }
      },
      submitedMenu(res) {
        this.getOrgTree()
        this.query = ''
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped="scope">
  .el-header, .el-footer {
    background-color: #B3C0D1;
    color: #333;
    text-align: center;
    line-height: 60px;
  }

  .el-aside {
    background-color: #D3DCE6;
    color: #333;
    text-align: center;
    line-height: 200px;
  }

  .el-main {
    background-color: #E9EEF3;
    color: #333;
    text-align: center;
    line-height: 160px;
  }

  body > .el-container {
    margin-bottom: 40px;
  }

  .el-container:nth-child(5) .el-aside,
  .el-container:nth-child(6) .el-aside {
    line-height: 260px;
  }

  .el-container:nth-child(7) .el-aside {
    line-height: 320px;
  }

  .el-header {
    background: #fff;
  }

  .el-main {
    background: #fff;
  }
</style>
